package com.chunuo.blogs.service.impl;

import com.chunuo.blogs.dao.BlogsClassifyDao;
import com.chunuo.blogs.dao.BlogsDao;
import com.chunuo.blogs.dao.BlogsTagDao;
import com.chunuo.blogs.entity.*;
import com.chunuo.blogs.service.BlogsService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Key;
import java.util.List;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-20 11:55
 */
@CacheConfig(cacheManager = "redisCacheManager",cacheNames = {"blogs"})
@Service
public class BlogsServiceImpl implements BlogsService {

    @Autowired
    private BlogsDao blogsDao;

    @Autowired
    private BlogsTagDao blogsTagDao;

    @Autowired
    private BlogsClassifyDao blogsClassifyDao;

    @Cacheable(key="#search",cacheNames = "blogsAll")
    @Override
    public List<Blogs> findAll(SearchBean search) {
        if(search.getTitle()!=null&&!search.getTitle().isEmpty())
            search.setTitle("%"+search.getTitle()+"%");
        PageHelper.startPage(search.getPage(),search.getSize());
        return blogsDao.findAll(search);
    }


    @Cacheable(key = "#id")
    @Override
    public Blogs findById(Integer id) {
        return blogsDao.findById(id);
    }

    @Cacheable(key="#page+'-'+#size",cacheNames = "findFiling")
    @Override
    public List<BlogsFIling> findFiling(Integer page,Integer size) {
        PageHelper.startPage(page,size);
        return blogsDao.findFiling();
    }

    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {@CacheEvict(value = "blogsAll",allEntries = true),@CacheEvict(value = "findFiling",allEntries = true)})
    @CachePut(key = "#result.id")
    @Override
    public Blogs save(Blogs blogs) {
        blogsDao.save(blogs);
        fieldUpdate(blogs);
        return blogs;
    }

    @Transactional(rollbackFor = Exception.class)
//    @CacheEvict(cacheNames = "blogsAll",allEntries = true)
    @Caching(evict = {@CacheEvict(value = "blogsAll",allEntries = true),@CacheEvict(value = "findFiling",allEntries = true)})
    @CachePut(key = "#result.id")
    @Override
    public Blogs update(Blogs blogs) {
        blogsTagDao.delete(blogs.getId());
        blogsClassifyDao.delete(blogs.getId());

        blogsDao.update(blogs);
        fieldUpdate(blogs);
        return blogs;
    }


    public void fieldUpdate(Blogs blogs){
        for(Classify classify :blogs.getBlogsClassifieList()){
            blogsClassifyDao.save(new BlogsClassify(0,blogs.getId(),classify.getId()));
        }
        for(Tag tag:blogs.getBlogsTagList()){
            blogsTagDao.save(new BlogsTag(0,blogs.getId(),tag.getId()));
        }
    }

//    @CacheEvict(cacheNames = "blogsAll",allEntries = true)
//    @CacheEvict(key = "#id")
    @Caching(evict = {@CacheEvict("#id"),@CacheEvict(value = "blogsAll",allEntries = true),@CacheEvict(value = "findFiling",allEntries = true)})
    @Override
    public void delete(Integer id) {
        blogsDao.delete(id);
    }
}
